/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : SPI Flash W25Qxx Example
 ******************************************************************************
 */

#include "stm32f4xx.h"
#include "./BSP/SPI/bsp_spi.h"
#include "./Devices/W25Qxx/bsp_w25qxx.h"
#include "./BSP/USART/bsp_usart.h"
#include <stdio.h>
#include <string.h>

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    usart_init(115200);
    spi_init();

    printf("SPI Flash W25Qxx Example\r\n");

    if (w25qxx_init() != 0)
    {
        printf("W25Qxx initialization failed\r\n");
        while (1);
    }

    uint32_t flash_id = w25qxx_read_id();
    printf("Flash ID: 0x%06X\r\n", flash_id);

    uint8_t write_data[] = "Hello W25Qxx Flash";
    w25qxx_write(0x0000, write_data, strlen((char *)write_data));
    printf("Data written\r\n");

    HAL_Delay(100);

    uint8_t read_data[32] = {0};
    w25qxx_read(0x0000, read_data, strlen((char *)write_data));
    printf("Data read: %s\r\n", (char *)read_data);

    while (1)
    {
        HAL_Delay(1000);
    }
}

void SystemClock_Config(void)
{
    /* 同上 */
}
